package com.zhongkun.datahome.service.service.impl;

import com.zhongkun.datahome.core.repository.SysDictDataRepository;
import com.zhongkun.datahome.service.constant.MaterialRoomRelationTypeEnum;
import com.zhongkun.datahome.service.constant.MaterialTypeEnum;
import com.zhongkun.datahome.service.constant.SysDictTypeEnum;
import com.zhongkun.datahome.service.domain.MaterialRoom;
import com.zhongkun.datahome.service.repository.DecorationStandardItemRepository;
import com.zhongkun.datahome.service.repository.HouseMaterialRepository;
import com.zhongkun.datahome.service.repository.MaterialRoomRepository;
import com.zhongkun.datahome.service.req.cmd.room.MaterialRoomCmd;
import com.zhongkun.datahome.service.res.room.MaterialRoomVo;
import com.zhongkun.datahome.service.service.IMaterialRoomService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/23 10:50
 * @Desc
 */
@Service
public class MaterialRoomServiceImpl implements IMaterialRoomService {
    @Resource
    private MaterialRoomRepository materialRoomRepository;

    @Resource
    private SysDictDataRepository sysDictDataRepository;

    @Resource
    private DecorationStandardItemRepository decorationStandardItemRepository;

    @Resource
    private HouseMaterialRepository houseMaterialRepository;

    @Override
    public List<MaterialRoomVo> listRoom(String relationType, Long relationId) {
        List<MaterialRoom> list = materialRoomRepository.listBy(relationType, relationId);
        List<MaterialRoomVo> result = list.stream().map(MaterialRoomVo::of).collect(Collectors.toList());

        // 排序
        sysDictDataRepository.sortBy(SysDictTypeEnum.HOUSE_MATERIAL_ROOM.getType(), result, MaterialRoomVo::getRoomCode);
        return result;
    }

    @Override
    public Long add(MaterialRoomCmd cmd) {
        MaterialRoom materialRoom = MaterialRoom.of(cmd);
        materialRoomRepository.save(materialRoom);
        return materialRoom.getId();
    }

    @Override
    @Transactional
    public void remove(List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
//            MaterialRoom materialRoom = materialRoomRepository.getById(ids.get(0));
//            if (MaterialRoomRelationTypeEnum.HOUSE.is(materialRoom.getRelationType())) {
//                houseMaterialRepository.removeByRoomIds(ids);
//            }
//            if (MaterialRoomRelationTypeEnum.STANDARD.is(materialRoom.getRelationType())) {
//                decorationStandardItemRepository.removeByRoomIds(ids);
//            }

            materialRoomRepository.removeBatchByIds(ids);
        }
    }
}
